package me.snowiest.snowdrift.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.PageList;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import me.snowiest.snowdrift.entity.Archive;
import me.snowiest.snowdrift.mapper.ArchiveMapper;
import me.snowiest.snowdrift.service.ArchiveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

@Transactional
@Service
public class ArchiveServiceImpl implements ArchiveService {

    @Autowired
    private ArchiveMapper archiveMapper;

    @Override
    public Archive getArchiveById(Integer id) {
        return archiveMapper.selectById(id);
    }

    @Override
    public List<Archive> listAllArchive() {
        return archiveMapper.selectList(null);
    }

    @Override
    public List<Archive> listAllArchive(int pages, int pageSize) {
        Page<Archive> archivePage = archiveMapper.selectPage(
                new Page<>(pages, pageSize),
                null
        );
        return archivePage.getRecords();
    }

    @Override
    public void addArchive(Archive archive) {
        archiveMapper.insert(archive);
    }

    @Override
    public void saveArchive(Archive archive) {
        UpdateWrapper<Archive> archiveUpdateWrapper = new UpdateWrapper<>();
        archiveUpdateWrapper.eq("id", archive.getId());
        archiveMapper.update(archive, archiveUpdateWrapper);
    }

    @Override
    public void removeArchiveById(Integer id) {
        archiveMapper.deleteById(id);
    }
}
